#!/usr/bin/env bash

. ./common.sh

TASK_NAME=task-performance-test-initializer

get_app_properties() {
  [[ -z "$ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_DEFINITION_COUNT" ]] &&  ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_DEFINITION_COUNT=10
  [[ -z "$ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_PREFIX" ]] &&  ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_PREFIX=perfTestTask
  [[ -z "$ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_ADD_TASK_EXECTIONS" ]] &&  ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_ADD_TASK_EXECUTIONS=true
  [[ -z "$ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_EXECUTION_COUNT" ]] &&  ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_EXECUTION_COUNT=10
  [[ -z "$ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_CLEANUP" ]] &&  ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_CLEANUP=false
  [[ -z "$ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_LAUNCH_TASKS" ]] &&  ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_LAUNCH_TASKS=false
  [[ -z "$ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_MAX_TASK_EXECUTION_WAIT_MINUTES" ]] &&  ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_MAX_TASK_EXECUTION_WAIT_MINUTES=15
  [[ -z "$ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_JOB_INSTANCES_PER_EXECUTION" ]] && $ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_JOB_INSTANCES_PER_EXECUTION=0
  return 0
}

create_manifest() {
  get_app_properties
  DB_SERVICE_INSTANCE=$(get_db_service_instance)
  cat << EOF > ./manifest.yml
applications:
- name: $TASK_NAME
  timeout: 120
  path: ./task-perf-tests-initializer/target/task-performance-tests-initializer-1.1.0.BUILD-SNAPSHOT.jar
  memory: 1G
  health-check: process
  no-route: true
  buildpack: $JAVA_BUILDPACK
  env:
    DATAFLOW_SERVER_URI: $SERVER_URI
    JBP_CONFIG_SPRING_AUTO_RECONFIGURATION: '{ enabled: false }'
    SPRING_PROFILES_ACTIVE: cloud
    SPRING_CLOUD_DATAFLOW_CLIENT_SERVER_URI: $SERVER_URI
    SPRING_CLOUD_DATAFLOW_CLIENT_AUTHENTICATION_CLIENT_SECRET: $SPRING_CLOUD_DATAFLOW_CLIENT_AUTHENTICATION_CLIENT_SECRET
    SPRING_CLOUD_DATAFLOW_CLIENT_AUTHENTICATION_CLIENT_ID: $SPRING_CLOUD_DATAFLOW_CLIENT_AUTHENTICATION_CLIENT_ID
    SPRING_CLOUD_DATAFLOW_CLIENT_AUTHENTICATION_TOKEN_URI: $SPRING_CLOUD_DATAFLOW_CLIENT_AUTHENTICATION_TOKEN_URI
    ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_DEFINITION_COUNT: $ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_DEFINITION_COUNT
    ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_PREFIX: $ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_PREFIX
    ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_ADD_TASK_EXECUTIONS: $ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_ADD_TASK_EXECUTIONS
    ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_EXECUTION_COUNT: $ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_EXECUTION_COUNT
    ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_CLEANUP: $ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_CLEANUP
    ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_LAUNCH_TASKS: $ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_LAUNCH_TASKS
    ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_MAX_TASK_EXECUTION_WAIT_MINUTES: $ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_MAX_TASK_EXECUTION_WAIT_MINUTES
    ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_JOB_INSTANCES_PER_EXECUTION: $ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_JOB_INSTANCES_PER_EXECUTION
  services:
   - $DB_SERVICE_INSTANCE
EOF
}


# Main
./mvnw clean package -f task-perf-tests-initializer
create_manifest
cf push -i 0
echo "CLEANING UP TASKS"
task_wait $TASK_NAME ".java-buildpack/open_jdk_jre/bin/java org.springframework.boot.loader.JarLauncher --org.springframework.cloud.dataflow.task.performance.cleanup=true"
echo "INITIALIZING $ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_DEFINITION_COUNT DEFINITIONS AND $ORG_SPRINGFRAMEWORK_CLOUD_DATAFLOW_TASK_PERFORMANCE_TASK_EXECUTION_COUNT EXECUTIONS PER DEFINITION"
task_wait $TASK_NAME ".java-buildpack/open_jdk_jre/bin/java org.springframework.boot.loader.JarLauncher"
cf delete -f $TASK_NAME






